import logging
from enum import Enum, unique

# 使用枚举
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, member in Month.__members__.items():
    print(name, "=>", member.value)  # Jan => 1 ... Dec => 12

@unique
class Weekday(Enum):
    Sun = 0  # Sun的value被设定为0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6
day1 = Weekday.Mon
print(day1, day1.value) # Weekday.Mon 1

# 使用元类：先定义metaclass，就可以创建类，最后创建实例。
class ListMetaclass(type):
    def __new__(cls, name, bases, attrs):
        attrs['add'] = lambda self, value: self.append(value)
        return type.__new__(cls, name, bases, attrs)
class MyList(list, metaclass=ListMetaclass):
    pass
L = MyList()
L.add(1)
L.add(2)
print(L) # [1, 2]

# 自带的日志框架
logging.basicConfig(level=logging.INFO, filename="../file/log.txt")
logging.info("hello world")

# 单元测试
import unittest

class TestDict(unittest.TestCase):
    def setUp(self):
        print("before")
    def test_init(self):
        d = {"name": "huhx", "age": 34}
        self.assertEqual(d.get("name"), "huhx")
    def test_add(self):
        d = {"name": "huhx", "age": 34}
        self.assertEqual(len(d), 1)
    def tearDown(self):
        print("after")
unittest.main() # 运行test case






